package com.itheima.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.itheima.domain.Courier;
import com.itheima.domain.Standard;
import com.itheima.service.CourierService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
@Controller
public class CourierAction extends ActionSupport implements ModelDriven<Courier>{

	private Courier courier = new Courier();
	
	@Override
	public Courier getModel() {
		return courier;
	}
	@Autowired
	private CourierService courierService;
	
	//重新重定向访问收派员这个Html 
	@Action(value="addAourier",results={@Result(name="success",location="./pages/base/courier.html",type="redirect")})
	public String addAourier(){
		//调用service
		System.out.println("add------------------------------------------------------------");
		courierService.save(courier);
		return SUCCESS;
	}
	
	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	@Action(value="page_courier",results={@Result(name="success",type="json")})
	public String page_courier(){
		//首先创造一个pageAble
		Pageable pageable = new PageRequest(page-1, rows);
		//然后封装一个Specification
		Specification<Courier> 	sf = new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list= new ArrayList<Predicate>();
				
				//单表查询
				//取派员的编号一样
				if(StringUtils.isNotBlank(courier.getCourierNum())){
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class),
							courier.getCourierNum());
					list.add(p1);
				}
				//取派员的公司一样
				if(StringUtils.isNotBlank(courier.getCompany())){
					Predicate p2 = cb.like(root.get("company").as(String.class),
							"%"+courier.getCompany()+"%");
					list.add(p2);
				}
				//取派员的类型一样
				if(StringUtils.isNotBlank(courier.getType())){
					Predicate p3 = cb.equal(root.get("type").as(String.class),
							courier.getType());
					list.add(p3);
				}
				//多表查询
				Join<Courier, Standard> join = root.join("standard",JoinType.INNER);
					if((courier.getStandard() != null)&& StringUtils.isNotBlank(courier.getStandard().getName())){
					Predicate p4=cb.like(join.get("name").as(String.class), "%"+courier.getStandard().getName()+"%");
						list.add(p4);
					}
					
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		//调用业务层查询数据 传过去一个pageable
		Page<Courier> date = courierService.findByPageData(sf,pageable);
		//然后将结果集封装到一个Map中去
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total",date.getTotalElements());
		result.put("rows", date.getContent());
		//将分页查询结果放入值栈去
		ActionContext.getContext().getValueStack().push(result);
		//条件查询
		
		return SUCCESS;
	}
	//属性注入ids
	private String ids;
	
	public void setIds(String ids) {
		this.ids = ids;
	}
	@Action(value="courier_delete",results={@Result(name="success",location="./pages/base/courier.html",type="redirect")})
	public String courier_delete(){
		//接收页面穿过来的ids
		//切割
		String[] strs = ids.split(",");
		courierService.delBatch(strs);
		return SUCCESS;
	}
	
}
